home *** CD-ROM | disk | FTP | other *** search
/ PsL Monthly 1993 December / PSL Monthly Shareware CD-ROM (December 1993).iso / prgmming / dos / c / printq.exe / PRINTQ.CPP < prev    next >
C/C++ Source or Header  |  1993-05-23  |  2KB  |  143 lines

  1. #include <tv.h>
  2. #include "PrintQ.h"
  3. #include <mem.h>
  4. #include <io.h>
  5. #include <fcntl.h>
  6. #include <share.h>
  7.  
  8. Boolean PrintQueue::insert(PrintJob* job)
  9.     {
  10.     if(job != 0)
  11.         {
  12.         job->reset();
  13.         if(count >= limit && !setJobsSize((long)limit + PQDelta))
  14.             return False;
  15.         jobs[count++] = job;
  16.         }
  17.     return True;
  18.     }
  19.  
  20. Boolean PrintQueue::kill(PrintJob* job)
  21.     {
  22.     int i;
  23.  
  24.     if(job != 0)
  25.         {
  26.         if(job == currentJob() && job->amountPrinted() > 0L)
  27.             {
  28.             printFormFeed();
  29.             job->reset();
  30.             }
  31.         for(i = 0; i < count; i++)
  32.             if(jobs[i] == job)
  33.                 {
  34.                 delete jobs[i];
  35.                 if(i < count-1)
  36.                     memmove(jobs+i, jobs+i+1, (count-i-1)*sizeof(PrintJob*));
  37.                 count--;
  38.                 return True;
  39.                 }
  40.         return False;
  41.         }
  42.     return True;
  43.     }
  44.  
  45. void PrintQueue::killAll()
  46.     {
  47.     while(count > 0)
  48.         kill(jobs[count-1]);
  49.     setJobsSize(0);
  50.     }
  51.  
  52. Boolean PrintQueue::print()
  53.     {
  54.     char* buffer;
  55.     int length, n;
  56.     PrintJob* job;
  57.  
  58.     job = currentJob();
  59.     if(job != 0)
  60.         {
  61.         length = job->getText(buffer);
  62.         if(buffer != 0 && length > 0)
  63.             {
  64.             n = printString(buffer,length);
  65.             job->skip(n);
  66.             if(n != length)
  67.                 return False;
  68.             }
  69.         if(job->complete())
  70.             kill(job);
  71.         }
  72.     return True;
  73.     }
  74.  
  75. Boolean PrintQueue::setJobsSize(int sz)
  76.     {
  77.     PrintJob** aJobs;
  78.  
  79.     if(sz >= PQMaxSize || sz < count)
  80.         return False;
  81.  
  82.     if(sz == 0)
  83.         aJobs = 0;
  84.     else
  85.         {
  86.         aJobs = new PrintJob* [sz];
  87.         if(aJobs == 0)
  88.             return False;
  89.         if(count > 0)
  90.             memcpy(aJobs, jobs, count*sizeof(PrintJob*));
  91.         }
  92.     delete [] jobs;
  93.     jobs = aJobs;
  94.     limit = sz;
  95.     return True;
  96.     }
  97.  
  98. // ------------------------------------------------------------------
  99.  
  100. PrintFile::PrintFile(const char* filespec, int aPrintSize) :
  101.         PrintJob(aPrintSize),
  102.         buffer(new char [aPrintSize]),
  103.         handle(-1)
  104.     {
  105.     if(buffer == 0)
  106.         size = 0;
  107.     else
  108.         {
  109.         handle = sopen(filespec, O_BINARY|O_RDONLY, SH_DENYWR);
  110.         if(handle < 0)
  111.             {
  112.             delete [] buffer;
  113.             buffer = 0;
  114.             size = 0;
  115.             }
  116.         else
  117.             size = filelength(handle);
  118.         }
  119.     }
  120.  
  121. PrintFile::~PrintFile()
  122.     {
  123.     if(handle >= 0)
  124.         close(handle);
  125.     delete [] buffer;
  126.     }
  127.  
  128. int PrintFile::getText(char*& buf)
  129.     {
  130.     if(complete())
  131.         {
  132.         buf = 0;
  133.         return 0;
  134.         }
  135.     else
  136.         {
  137.         buf = buffer;
  138.         lseek(handle, pos, SEEK_SET);
  139.         return read(handle, buffer, printSize);
  140.         }
  141.     }
  142.  
  143.